package co.edu.usbcali.egrapico.vista.ambient;

public class LPaneProgram extends javax.swing.JPanel {//implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener {

    private java.util.Vector figures;
    private java.util.Vector Undo;
    private boolean ban = false;
    private java.awt.Dimension area;
    private java.awt.Component dragComponent;
    private int xAdjustment;
    private int yAdjustment;

    /**
     * Creates a new instance of LPaneProgram
     */
    public LPaneProgram() {
        figures = new java.util.Vector();
        Undo = new java.util.Vector();
        area = new java.awt.Dimension(0, 0);
        setLayout(null);
        setBackground(new java.awt.Color(255, 255, 255));
        setToolTipText("Program Ambient");

        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mousePressed(java.awt.event.MouseEvent evt) {
                LPMousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                LPMouseReleased(evt);
            }

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LPMouseClicked(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

            public void mouseDragged(java.awt.event.MouseEvent evt) {
                LPMouseDragged(evt);
            }

            public void mouseMoved(java.awt.event.MouseEvent evt) {
                System.out.println(evt.getX() + " - " + evt.getY());
                LPMouseMoved(evt);
            }
        });
    }

    //methods 
    public void LPMouseMoved(java.awt.event.MouseEvent evt) {
        java.awt.Component c = getComponentAt(evt.getX(), evt.getY());
        if (c instanceof LabelContext) {
            setToolTipText(((LabelContext) c).getTextToolTip());
        } else if (c instanceof LabelAsk) {
            setToolTipText(((LabelAsk) c).getTextToolTip());
        } else if (c instanceof LabelTell) {
            setToolTipText(((LabelTell) c).getTextToolTip());
        } else if (c instanceof LabelMsgSend) {
            setToolTipText(((LabelMsgSend) c).getTextToolTip());
        } else if (c instanceof LabelObject) {
            setToolTipText(((LabelObject) c).getTextToolTip());
        } else {
            setToolTipText("Program Ambient");
        }
        repaint();
    }

    public void LPMouseClicked(java.awt.event.MouseEvent evt) {
        java.awt.Component c = getComponentAt(evt.getX(), evt.getY());
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            if (c instanceof LabelContext) {
                ((LabelContext) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            } else if (c instanceof LabelAsk) {
                ((LabelAsk) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            } else if (c instanceof LabelProgram) {
                ((LabelProgram) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            } else if (c instanceof LabelTell) {
                ((LabelTell) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            } else if (c instanceof LabelMsgSend) {
                ((LabelMsgSend) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            } else if (c instanceof LabelObject) {
                ((LabelObject) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
        if (evt.getClickCount() == 2) {

            if (c instanceof LabelContext) {

                if (((LabelContext) c).isCreate() == false) {
                    ((LabelContext) c).Ladicionar();
                } else {
                    ((LabelContext) c).mostrarPanel();
                }

            } else if (c instanceof LabelAsk) {

                if (((LabelAsk) c).isCreate() == false) {
                    ((LabelAsk) c).Ladicionar();
                } else {
                    ((LabelAsk) c).mostrarPanel();
                }

            } else if (c instanceof LabelTell) {

                if (((LabelTell) c).isCreate() == false) {
                    ((LabelTell) c).Ladicionar();
                } else {
                    ((LabelTell) c).mostrarPanel();
                }

            } else if (c instanceof LabelMsgSend) {

                if (((LabelMsgSend) c).isCreate() == false) {
                    ((LabelMsgSend) c).Ladicionar();
                } else {
                    ((LabelMsgSend) c).mostrarPanel();
                }

            } else if (c instanceof LabelObject) {

                if (((LabelObject) c).isCreate() == false) {
                    ((LabelObject) c).Ladicionar();
                } else {
                    ((LabelObject) c).mostrarPanel();
                }

            }
        }
    }

    public void LPMousePressed(java.awt.event.MouseEvent evt) {
        dragComponent = null;
        java.awt.Component c = getComponentAt(evt.getX(), evt.getY());
        if (c instanceof javax.swing.JPanel) {
            return;
        }
        if (c instanceof LabelContext) {
            if (((LabelContext) c).isClon()) {
                ((LabelContext) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/cloncontex1.gif")));
            } else {
                ((LabelContext) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/context1.gif")));
            }
            ApIcons(c);
        } else if (c instanceof LabelObject) {
            if (((LabelObject) c).isClon()) {
                ((LabelObject) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clon_object1.gif")));
            } else {
                ((LabelObject) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/object1.gif")));
            }
            ApIcons(c);
        } else if (c instanceof LabelAsk) {
            if (((LabelAsk) c).getIsConsecuent()) {
                if (((LabelAsk) c).isClon()) {
                    ((LabelAsk) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonask1.gif")));
                } else {
                    ((LabelAsk) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/ask1.gif")));
                }
            } else {
                if (((LabelAsk) c).isClon()) {
                    ((LabelAsk) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clon_ask.gif")));
                } else {
                    ((LabelAsk) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/askPreceding1.gif")));
                }

            }
            ApIcons(c);
        } else if (c instanceof LabelTell) {
            if (((LabelTell) c).getIsConsecuent()) {
                if (((LabelTell) c).isClon()) {
                    ((LabelTell) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clontellp1.gif")));
                } else {
                    ((LabelTell) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/tell1.gif")));
                }
            } else {
                if (((LabelTell) c).isClon()) {
                    ((LabelTell) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clontell1.gif")));
                } else {
                    ((LabelTell) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/tellPreceding1.gif")));
                }
            }
            ApIcons(c);
        } else if (c instanceof LabelMsgSend) {
            if (((LabelMsgSend) c).getIsConsecuent()) {
                if (((LabelMsgSend) c).isClon()) {
                    ((LabelMsgSend) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonmsgsend1.gif")));
                } else {
                    ((LabelMsgSend) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/msgSend1.gif")));
                }
            } else {
                if (((LabelMsgSend) c).isClon()) {
                    ((LabelMsgSend) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonmsgSendPreceding1.gif")));
                } else {
                    ((LabelMsgSend) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/msgSendPreceding1.gif")));
                }
            }
            ApIcons(c);
        }
        dragComponent = c;
        xAdjustment = dragComponent.getLocation().x - evt.getX();
        yAdjustment = dragComponent.getLocation().y - evt.getY();
        dragComponent.setLocation(evt.getX() + xAdjustment, evt.getY() + yAdjustment);

        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON1) {
            ban = true;
        } else if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            ban = false;
        }
    }

    public void ApIcons(java.awt.Component c) {
        for (int i = 0; i < getComponentCount(); i++) {
            if (!getComponent(i).equals(c)) {
                if (getComponent(i) instanceof LabelContext) {
                    if (((LabelContext) getComponent(i)).isClon()) {
                        ((LabelContext) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/cloncontex.gif")));
                    } else {
                        ((LabelContext) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/context.gif")));
                    }
                } else if (getComponent(i) instanceof LabelObject) {
                    if (((LabelObject) getComponent(i)).isClon()) {
                        ((LabelObject) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clon_object.gif")));
                    } else {
                        ((LabelObject) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/object.gif")));
                    }
                } else if (getComponent(i) instanceof LabelAsk) {
                    if (((LabelAsk) getComponent(i)).getIsConsecuent()) {
                        if (((LabelAsk) getComponent(i)).isClon()) {
                            ((LabelAsk) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonask.gif")));
                        } else {
                            ((LabelAsk) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/ask.gif")));
                        }
                    } else {
                        if (((LabelAsk) getComponent(i)).isClon()) {
                            ((LabelAsk) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clon_ask1.gif")));
                        } else {
                            ((LabelAsk) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/askPreceding.gif")));
                        }
                    }
                } else if (getComponent(i) instanceof LabelTell) {
                    if (((LabelTell) getComponent(i)).getIsConsecuent()) {
                        if (((LabelTell) getComponent(i)).isClon()) {
                            ((LabelTell) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clontellp.gif")));
                        } else {
                            ((LabelTell) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/tell.gif")));
                        }
                    } else {
                        if (((LabelTell) getComponent(i)).isClon()) {
                            ((LabelTell) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clontell.gif")));
                        } else {
                            ((LabelTell) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/tellPreceding.gif")));
                        }
                    }
                } else if (getComponent(i) instanceof LabelMsgSend) {
                    if (((LabelMsgSend) getComponent(i)).getIsConsecuent()) {
                        if (((LabelMsgSend) getComponent(i)).isClon()) {
                            ((LabelMsgSend) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonmsgsend.gif")));
                        } else {
                            ((LabelMsgSend) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/msgSend.gif")));
                        }
                    } else {
                        if (((LabelMsgSend) getComponent(i)).isClon()) {
                            ((LabelMsgSend) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonmsgSendPreceding.gif")));
                        } else {
                            ((LabelMsgSend) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/msgSendPreceding.gif")));
                        }
                    }
                }
            }
        }
    }

    public void LPMouseDragged(java.awt.event.MouseEvent evt) {
        int evtX = evt.getX();
        int evtY = evt.getY();
        if (dragComponent == null) {
            return;
        }

        if (ban) {
            if (evtX + xAdjustment < 0) {
                evtX = 0;
                xAdjustment = 0;
            }
            if (evtY + yAdjustment < 0) {
                evtY = 0;
                yAdjustment = 0;
            }
            dragComponent.setLocation(evtX + xAdjustment, evtY + yAdjustment);
            /**
             * ***********************verificacion del escroll***************
             */
            final int W = 100;
            final int H = 100;
            int x = evtX - W / 2;
            int y = evtY - H / 2;
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
            java.awt.Rectangle rect = new java.awt.Rectangle(x, y, W, H);
            scrollRectToVisible(rect);

            int this_width = (x + W + 2);
            if (this_width > area.width) {
                area.width = this_width;
            }

            int this_height = (y + H + 2);
            if (this_height > area.height) {
                area.height = this_height;
            }
            //Update client's preferred size because
            //the area taken up by the graphics has
            //gotten larger or smaller (if cleared).
            setPreferredSize(area);
            //Let the scroll pane know to update itself
            //and its scrollbars.
            revalidate();
        }
    }

    public void LPMouseReleased(java.awt.event.MouseEvent evt) {
        dragComponent = null;
        repaint();
    }

    public void paintComponent(java.awt.Graphics g) {
        //super.paintAll(g);
        super.paintComponent(g);

    }

    public void addLabeltoVector(LabelContext labe) {
        figures.add(labe);
    }

    public void addLabel(LabelContext labe) {
        add(labe);
        revalidate();
        repaint();
    }

    public void addLabeltoVector(LabelAsk labe) {
        figures.add(labe);
    }

    public void addLabel(LabelAsk labe) {
        add(labe);
        revalidate();
        repaint();
    }

    public void addLabeltoVector(LabelTell labe) {
        figures.add(labe);
    }

    public void addLabel(LabelTell labe) {
        add(labe);
        revalidate();
        repaint();
    }

    public void addLabeltoVector(LabelMsgSend labe) {
        figures.add(labe);
    }

    public void addLabel(LabelMsgSend labe) {
        add(labe);
        revalidate();
        repaint();
    }

    public void addLabeltoVector(LabelObject labe) {
        figures.add(labe);
    }

    public void addLabel(LabelObject labe) {
        add(labe);
        revalidate();
        repaint();
    }

    public void Undo() {
        Undo.add(figures.lastElement());
        int i = figures.size();
        if ((figures.get(i - 1) instanceof LabelContext)
                || (figures.get(i - 1) instanceof LabelAsk)
                || (figures.get(i - 1) instanceof LabelTell)
                || (figures.get(i - 1) instanceof LabelMsgSend)
                || (figures.get(i - 1) instanceof LabelObject)) {
            remove((java.awt.Component) figures.get(i - 1));
        }
        figures.remove(i - 1);
        repaint();
    }

    public int getUndoSize() {
        return Undo.size();
    }

    public void Redo() {
        if ((Undo.lastElement() instanceof LabelContext)
                || (Undo.lastElement() instanceof LabelAsk)
                || (Undo.lastElement() instanceof LabelTell)
                || (Undo.lastElement() instanceof LabelMsgSend)
                || (Undo.lastElement() instanceof LabelObject)) {
            add((java.awt.Component) Undo.lastElement());
        }
        figures.add(Undo.lastElement());
        int i = Undo.size();
        Undo.remove(i - 1);
        repaint();
    }

    public int getRedoSize() {
        return figures.size();
    }

    public java.awt.Dimension areaLPane() {
        return area;
    }

    public void setAreaLP(java.awt.Dimension ar) {
        area = ar;
    }
}